package com.hust360.percent.adapter;

import java.text.NumberFormat;
import java.util.List;

import com.hust360.percent.PercentApplication;
import com.hust360.percent.R;
import com.hust360.percent.pojo.DiscountInfo;
import com.hust360.percent.pojo.Shop;
import com.hust360.percent.utils.PercentThreadPool;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class StoreListAdapter extends BaseAdapter {

	private List<DiscountInfo> list;

	private Context context;
	private LayoutInflater inflater;

	private NumberFormat format;
	@SuppressLint("SdCardPath")
	String imgParam = "//mnt//sdcard//file//";

	private ListView listView;
	PercentApplication application;

	public StoreListAdapter(Context context) {
		super();
		this.context = context;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		format = NumberFormat.getIntegerInstance();
		application = (PercentApplication) context.getApplicationContext();
	}

	public StoreListAdapter(Context context, List<DiscountInfo> list,
			AdapterView<BaseAdapter> adapterView) {
		this.context = context;
		this.list = list;
		this.inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		format = NumberFormat.getIntegerInstance();
	}

	@Override
	public int getCount() {
		return list == null ? 0 : list.size();
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ItemHolder holder = null;
		DiscountInfo info = list.get(position);
		if (convertView == null) {
			holder = new ItemHolder();
			convertView = inflater.inflate(R.layout.store_item, null);
			holder.shopName = (TextView) convertView
					.findViewById(R.store_item.shopName);
			holder.titleTextView = (TextView) convertView
					.findViewById(R.store_item.title);
			holder.discountType = (TextView) convertView
					.findViewById(R.store_item.discount);
			holder.distance = (TextView) convertView
					.findViewById(R.store_item.distance);
			holder.liked = (TextView) convertView
					.findViewById(R.store_item.liked_count);
			holder.logoImageView = (ImageView) convertView
					.findViewById(R.store_item.logo);
			holder.shopAddress = (TextView) convertView
					.findViewById(R.store_item.address);
			convertView.setTag(holder);
		} else {
			holder = (ItemHolder) convertView.getTag();
			System.out.println(convertView.getParent()
					+ " the parent of convertView");
		}

		Shop shop = ((PercentApplication) context.getApplicationContext())
				.getShopDao().findById(info.getShopId());
		if (shop != null) {
			holder.shopName.setText(shop.getShopName());
			holder.shopAddress.setText(shop.getAddress());
		}

		/*
		 * 设置图片为默认的图片
		 */
		holder.logoImageView.setImageDrawable(application.getResources()
				.getDrawable(R.drawable.defaut_logo));

		/*
		 * 异步加载图片 
		 */
		List<String> imglist = info.getDiscountImage();
		if (imglist != null && imglist.size() > 0) {
			for (String string : imglist) {
				Log.i("StoreListAdapter", "id is " + info.getDiscountInfoId()
						+ " position is " + position
						+ " the image filename is " + string);
			}
			holder.logoImageView.setTag(position + "mTag");
			application.getThreadPool().loadDrawable("",
					imgParam + imglist.get(0),
					new LogoCallBack(position + "mTag", convertView));
		}

		holder.liked.setText(info.getLiked() + "人");
		holder.titleTextView.setText(info.getDiscountInfo());
		String typeStr = info.getDiscountType() == 0 ? "全部" : "部分";
		typeStr += info.getDiscount() + "折";
		holder.discountType.setText(typeStr);
		holder.distance.setText(format.format(info.getDistance()) + "米");
		return convertView;
	}

	public List<DiscountInfo> getList() {
		return list;
	}

	public void setList(List<DiscountInfo> list) {
		this.list = list;
	}

	public ListView getListView() {
		return listView;
	}

	public void setListView(ListView listView) {
		this.listView = listView;
	}

	private class ItemHolder {

		public ItemHolder() {
		}

		ImageView logoImageView;
		TextView shopName;
		TextView titleTextView;
		TextView discountType;
		TextView shopAddress;
		TextView distance;
		TextView liked;
	}

	private class LogoCallBack implements PercentThreadPool.DoneLoadingCallback {

		String tag;
		View converView;

		public LogoCallBack(String tag, View convertView) {
			this.tag = tag;
			this.converView = convertView;
		}

		@Override
		public void doneLoading(Drawable drawable) {
			if (drawable != null) {
				synchronized (converView) {
					Log.i(tag, "synchronized block run");
					ImageView imageView = (ImageView) converView
							.findViewWithTag(tag);
					if (imageView != null) {
						drawable.setBounds(0,0,imageView.getWidth(), imageView.getHeight());
						imageView.setImageDrawable(drawable);
						imageView.postInvalidate();
					} else {
						Log.e("TAG", tag.toString() + " imageView was null");
					}
				}
			}
		}

	}

}
